import * as types from './mutation-types'
import { saveSearchKey, clearSearch, savePlaySong, addFavoriteSong, delectFavoriteSong } from 'common/js/cache'
import { playMode, ADD_NS_STRING } from 'common/js/config'
import { shuffle, setNumOnRange } from 'common/js/util'

function findIndex(list, song) {
    return list.findIndex((item) => {
        return item.id === song.id
    })
}

export const selectPlay = function ({commit, state}, {list, index}) {
    commit(types.SET_SEQUENCE_LIST, list)
    if (state.mode === playMode.random) {
        let randomList = shuffle(list)
        commit(types.SET_PLAY_LIST, randomList)
        index = findIndex(randomList, list[index])
    } else {
        commit(types.SET_PLAY_LIST, list)
    }
    commit(types.SET_CURRENT_INDEX, index)
    commit(types.SET_FULL_SCREEN, true)
    commit(types.SET_PLAYING, true)
}

export const playAll = function ({commit}, {list}) {
    commit(types.SET_PLAY_MODE, playMode.sequence)
    commit(types.SET_SEQUENCE_LIST, list)
    commit(types.SET_PLAY_LIST, list)
    commit(types.SET_CURRENT_INDEX, 0)
    commit(types.SET_FULL_SCREEN, true)
    commit(types.SET_PLAYING, true)
}

// 将歌曲插入下一个
export const addNextSong = function ({commit, state}, song) {
    let check = isSongPlaying(state, song)

    if (check) {
        return
    }

    let { playList, sequenceList, currentIndex } = insertData(state, song)

    if (currentIndex > -1) {
        commit(types.SET_CURRENT_INDEX, currentIndex)
    } else {
        commit(types.SET_CURRENT_INDEX, 0)
        commit(types.SET_PLAYING, true)
    }

    commit(types.SET_PLAY_LIST, playList)
    commit(types.SET_SEQUENCE_LIST, sequenceList)
    commit(types.SET_TOAST_MSG, ADD_NS_STRING)
}

// 将歌曲插入下一个，并播放插入歌曲
export const insertSong = function ({state, commit}, song) {
    let { playList, sequenceList, currentPlIndex } = insertData(state, song)

    commit(types.SET_PLAY_LIST, playList)
    commit(types.SET_SEQUENCE_LIST, sequenceList)
    commit(types.SET_CURRENT_INDEX, currentPlIndex)
    commit(types.SET_PLAYING, true)
}

function insertData (state, song) {
    let playList = state.playList.slice()
    let sequenceList = state.sequenceList.slice()
    let currentIndex = state.currentIndex

    /*  ----------------插入到当前播放列表----------------  */

    // 查找插入歌曲是否在播放列表,并返回其索引
    let insertSongIndexInPl = findIndex(playList, song)

    // 歌曲要添加的位置（当前播放歌曲在顺序列表的下一个位置）
    let currentPlIndex = currentIndex + 1

    // 插入到当前播放的下一首
    playList.splice(currentPlIndex, 0, song)

    // 当插入歌曲插入前已经在歌曲列表中时，删除旧的歌曲
    if (insertSongIndexInPl > -1) {
        if (currentPlIndex > insertSongIndexInPl) {
            playList.splice(insertSongIndexInPl, 1)
            // 用于之后改变当前播放歌曲的索引值
            currentIndex--
            currentPlIndex--
        } else {
            playList.splice(insertSongIndexInPl + 1, 1)
        }
    }

    /*  ----------------插入到顺序播放列表----------------  */
    // 记录当前播放歌曲
    let currentSong = playList[currentIndex]

    // 歌曲要添加的位置（当前播放歌曲在顺序列表的下一个位置）
    let currentSlIndex = findIndex(sequenceList, currentSong) + 1

    // 查找插入歌曲是否在播放列表,并返回其索引
    let insertSongIndexInSq = findIndex(sequenceList, song)

    // 插入到顺序播放列表
    sequenceList.splice(currentSlIndex, 0, song)

    // 当插入歌曲插入前已经在歌曲列表中时，删除旧的歌曲
    if (insertSongIndexInSq > -1) {
        if (currentSlIndex > insertSongIndexInSq) {
            sequenceList.splice(insertSongIndexInSq, 1)
        } else {
            sequenceList.splice(insertSongIndexInSq + 1, 1)
        }
    }

    return {
        playList,
        sequenceList,
        currentPlIndex,
        currentIndex
    }
}

// 检查歌曲是否正在播放
function isSongPlaying(state, song) {
    let playList = state.playList
    let currentIndex = state.currentIndex
    let playSong = playList[currentIndex]

    if (playSong && playSong.id === song.id) {
        return true
    }

    return false
}

export const saveSearchHistory = function ({commit}, query) {
    commit(types.SET_SEARCH_HISTORY, saveSearchKey(query))
}

export const clearSearchHistory = function ({commit}) {
    commit(types.SET_SEARCH_HISTORY, clearSearch())
}

export const deleteSong = function ({commit, state}, song) {
    let playList = state.playList.slice()
    let sequenceList = state.sequenceList.slice()
    let currentIndex = state.currentIndex

    // 歌曲在正在播放歌曲列表位置
    let pIndex = findIndex(playList, song)
    // 歌曲在歌曲列表位置
    let sIndex = findIndex(sequenceList, song)

    // 删除歌曲
    playList.splice(pIndex, 1)
    sequenceList.splice(sIndex, 1)

    if (currentIndex >= pIndex) {
        currentIndex = setNumOnRange(currentIndex - 1, playList.length, 0)
    }

    commit(types.SET_PLAY_LIST, playList)
    commit(types.SET_SEQUENCE_LIST, sequenceList)
    commit(types.SET_CURRENT_INDEX, currentIndex)

    const isPlaying = playList.length > 0
    commit(types.SET_PLAYING, isPlaying)
}

export const clearSongList = function ({commit}) {
    commit(types.SET_SEQUENCE_LIST, [])
    commit(types.SET_PLAY_LIST, [])
    commit(types.SET_CURRENT_INDEX, -1)
    commit(types.SET_PLAYING, false)
}

export const savePlayHistory = function ({commit}, song) {
    commit(types.SET_PLAY_HISTORY, savePlaySong(song))
}

export const setFavorite = function ({commit}, {song, isFavorite}) {
    let songs = []
    if (isFavorite) {
        songs = addFavoriteSong(song)
        commit(types.SET_TOAST_MSG, '已收藏到我喜欢')
    } else {
        songs = delectFavoriteSong(song)
        commit(types.SET_TOAST_MSG, '已取消收藏')
    }
    commit(types.SET_FAVORITR_LIST, songs)
}

export const showConfirm = ({commit}, option) => {
    commit(types.SET_CONFIRM_OPTION, option)
}
export const clearConfirm = ({commit}) => {
    commit(types.SET_CONFIRM_OPTION, null)
}
